import { queryDrawRes } from "@/api/drawHist";
import { updateParams } from "@/components/Overlay/hooks/useUtils";
import { useFetchYrRefs } from "./useFetchYr";
import useDateList, {
  useDateListRefs
} from "@/components/Overlay/components/DatePicker/useDateList";
interface DateRollType {
  toggleShow(): void;
}
interface DrawDayType {
  draw_date?: string;
  draw_year?: string;
}
const useFetchWinRes = defineStore("drawHist-fetchWinRes", () => {
  const { curYear } = useFetchYrRefs();
  const { findDrawIdByDate } = useDateList();
  useDateListRefs();
  const winResList = ref<any[]>([]);
  const winResParams = reactive({
    draw_id: "",
    draw_year: "",
    draw_day: "",
    draw_level: ""
  });
  const dateRef = ref<DateRollType>();
  function toggleDate() {
    dateRef.value && dateRef.value.toggleShow();
  }

  function updateDate(it: DrawDayType) {
    updateParams(
      {
        draw_id: findDrawIdByDate(it.draw_date as string)
      },
      winResParams
    );
    updateParams(
      {
        draw_year: curYear.value
      },
      winResParams
    );
    fetchWinRes();
  }

  async function fetchWinRes() {
    // console.log("fetchWinRes", winResParams);
    const res = await queryDrawRes(winResParams);
    winResList.value = res?.data ?? [];
  }

  return {
    winResList,
    winResParams,
    toggleDate,
    dateRef,
    updateDate,
    fetchWinRes
  };
});
export const useFetchWinResRefs = () => storeToRefs(useFetchWinRes());
export default useFetchWinRes;
